package thread;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.http.NameValuePair;

import bo.IsConnected;
import bo.UserFunctions;

import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.gson.Gson;
import com.steam.splace.R;
import com.steam.splace.SearchActivity;
import com.steam.splace.R.id;

import entity.Category;
import entity.Comment;
import entity.Message;
import entity.Place;
import entity.Region;
import entity.User;


import android.R.integer;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.AsyncTask;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.SubMenu;
import android.webkit.WebView.FindListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class LoadLoggedIn extends AsyncTask<Void, ArrayList, Void>{
    private int tag=-1;
    private final int loadtag=100;
    Activity contextParent;
    ProgressDialog progress; 
	 public LoadLoggedIn(Activity ctx)
	 {
		 contextParent=ctx;
		 progress = new ProgressDialog(ctx);
	 }
 @Override
	  protected void onPreExecute() { 
	    progress.setMessage("Please wait");
	    progress.setIndeterminate(true);
	   progress.setCancelable(false);
	    progress.show();   
	  }
	@Override
	protected Void doInBackground(Void... arg0) {
		// TODO Auto-generated method stub
		IsConnected ic=new IsConnected(contextParent);
		if(ic.isConnectedToInternet()==true){
			ArrayList loadResult=new ArrayList();
		UserFunctions user=new UserFunctions();
		user.setActivity(contextParent);
		loadResult.add(user.getCategory());
			loadResult.add(user.getRegion());
			publishProgress(loadResult);
		}
		else {
			ArrayList arr=new ArrayList();
			publishProgress(arr);tag=-404;}
		return null;
	}
	@Override
	 protected void onProgressUpdate(ArrayList... values){
		
		if(tag==-404){
			if(progress.isShowing())
			      progress.dismiss();
			Toast toast=Toast.makeText(contextParent, "Vui lòng kiểm tra đường truyền mạng!",
				500);
		toast.setGravity(Gravity.CENTER, 0, 0);
		toast.show();}
		else if(values[0]!=null&&values[0].size()==2){
			
			SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(contextParent);
			 Editor prefsEditor = prefs.edit();
		     Gson gson = new Gson();
		     int catSize=((ArrayList<Category>)values[0].get(0)).size();
		     int regionSize=((ArrayList<Region>)values[0].get(1)).size();
		     prefsEditor.putInt("categorysize",catSize);
		     prefsEditor.putInt("regionsize",regionSize);
		     for(int i=0;i<catSize;i++){
		    	 String json1 = gson.toJson(((ArrayList<Category>)values[0].get(0)).get(i));
			     prefsEditor.putString("category_"+i, json1);
		     }
		     for(int i=0;i<regionSize;i++){
		    	 String json1 = gson.toJson(((ArrayList<Region>)values[0].get(1)).get(i));
			     prefsEditor.putString("region_"+i, json1);
		     }
		     prefsEditor.commit();
		     int uid = prefs.getInt("uid", -1);		  
		    // System.out.println("Uid from log:"+uid);
		     if(uid!=-1){
		     Intent searchIntent = new Intent(contextParent, SearchActivity.class);
		     contextParent.startActivity(searchIntent);
		     }
		     else{
		    	 contextParent.setContentView(R.layout.activity_main);
		     }
		}
		if(progress.isShowing())
		      progress.dismiss();
	}
}
